<?php

include('includes/before_html.php');
$db = mysql_connect(db_server, $db_user, $db_password);
mysql_select_db($db_dbname);
echo '<div style="background-color:#ffffff; padding:20px">';
$excluded = array('Person1', 'id', 'RUID');
$tables = array('Users', 'Housing', 'Relationships');

foreach ($tables as $table) {
    $submit = 0;
    $result = mysql_query("describe $table");
    $colnames = '';
    $values = '';
    $update = '';
    while ($row = mysql_fetch_array($result)) {
        if (!in_array($row['Field'], $excluded)) {
            $colnames.=$row['Field'] . ",";
            $val = $_POST[$table . "_" . $row['Field']];
            if (!empty($val) || $val != '') {
                $submit = 1;
                $values.= "'" . $val . "',";
                $update.=$row['Field'] . "='" . $val . "',";
            } else {
                $values.="NULL,";
                $update.=$row['Field'] . "=NULL,";
            }
            
        }
    }
    if ($table == 'Relationships') {
        $colnames.="Person1";
    } else {
        $colnames.="RUID";
    }

    $values.=$myRuid;

    $update = substr($update, 0, strlen($update) - 1);
    $sql = "INSERT INTO $table ($colnames) VALUES ($values) ON DUPLICATE KEY UPDATE $update";

    if ($submit == 1) {
        echo "SQL QUERY: " . $sql . "<BR><BR>";
        $result = mysql_query($sql);
        if ($result)
            echo "<strong>Profile settings saved.</strong><br><br>";
        else
            echo "<strong>ERROR: ".mysql_error()."</strong><br><br>";
    }
}
echo '</div>';
?>
